package com.syas.service.services;

import com.syas.base.service.services.EntityService;
import com.syas.common.extension.StringHelper;
import com.syas.common.utilities.PageList;
import com.syas.common.utilities.PageListUtil;
import com.syas.dao.interfaces.ISalcalDao;
import com.syas.model.models.FmgerCalResult;
import com.syas.service.interfaces.ISalcalService;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;


@Service("SalcalService")
@Transactional()
class SalcalService extends EntityService<Integer, FmgerCalResult, ISalcalDao> implements ISalcalService{

    @Autowired
    public SalcalService(@Qualifier("SalcalDao") ISalcalDao salcalDao) {
        super(salcalDao);
    }
    @Override
    public PageList<FmgerCalResult> listPage(String ezubouname, String certid, int pageNo, int pageSize) {
        Criteria countCriteria = entityDao.getCriteria();
        Criteria listCriteria = entityDao.getCriteria();
        if(!StringHelper.isEmpty(ezubouname)){
            countCriteria.add(Restrictions.eq("ezboUname", ezubouname));
            listCriteria.add(Restrictions.eq("ezboUname", ezubouname));
        }
        if(!StringHelper.isEmpty(certid)){
            countCriteria.add(Restrictions.eq("certId", certid));
            listCriteria.add(Restrictions.eq("certId", certid));
        }
        listCriteria.setFirstResult((pageNo-1)*pageSize);
        listCriteria.setMaxResults(pageSize);
        List<FmgerCalResult> items = listCriteria.list();
        countCriteria.setProjection(Projections.rowCount());
        Integer count=StringHelper.convertStringToInt(countCriteria.uniqueResult());
        return PageListUtil.getPageList(count, pageNo, items, pageSize);
    }
    public List<FmgerCalResult> listPage(String ezubouname, String certid) {
        Criteria countCriteria = entityDao.getCriteria();
        Criteria listCriteria = entityDao.getCriteria();
        if(!StringHelper.isEmpty(ezubouname)){
            countCriteria.add(Restrictions.eq("ezboUname", ezubouname));
            listCriteria.add(Restrictions.eq("ezboUname", ezubouname));
        }
        if(!StringHelper.isEmpty(certid)){
            countCriteria.add(Restrictions.eq("certId", certid));
            listCriteria.add(Restrictions.eq("certId", certid));
        }
        List<FmgerCalResult> items = listCriteria.list();
        return items;
    }
}
